﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using System.IO;

namespace DiscreteMath.WebApp
{
    public static class JsonConverter
    {
        public static string ToJson(this NondeterministicAutomaton automaton)
        {
            char[] alphabetSorted = automaton.Alphabet.OrderBy(x => x).ToArray();
            TextWriter text = new StringWriter();
            JsonWriter writer = new JsonTextWriter(text);
            writer.WriteStartObject();
            writer.WritePropertyName("alphabet");
            writer.WriteStartArray();
            //writer.WriteRawValue(alphabetSorted.Print(x => string.Format("\"{0}\"", x), false));
            foreach (char letter in alphabetSorted)
            {
                writer.WriteValue(letter);
            }
            writer.WriteEndArray();
            writer.WritePropertyName("transitions");
            writer.WriteStartArray();

            foreach (State state in automaton.States)
            {
                writer.WriteStartObject();
                writer.WritePropertyName("state");
                writer.WriteValue(state);
                writer.WritePropertyName("nextStates");
                writer.WriteStartArray();
                for (int i = 0; i < alphabetSorted.Length; i++)
                {
                    ISet<State> transition = automaton.Transitions[state, alphabetSorted[i]];
                    writer.WriteStartArray();
                    foreach (State next in transition)
                    {
                        writer.WriteValue(next);
                    }
                    writer.WriteEndArray();
                }
                writer.WriteEndArray();
                writer.WriteEndObject();
            }
            writer.WriteEndArray();
            writer.WriteEndObject();
            writer.Flush();


            JsonReader reader = new JsonTextReader(new StringReader(text.ToString()));

            return text.ToString();
        }

        public static NondeterministicAutomaton FromJson(this string json)
        {
            JsonReader reader = new JsonTextReader(new StringReader(json));
            reader.Read();
            reader.Read();
            reader.Read();
            IList<char> alphabet = new List<char>();
            reader.Read();
            while (reader.Value != null)
            {
                alphabet.Add((char)reader.Value);
                reader.Read();
            }
            reader.Read();
            reader.Read();
            ISet<State> states = new HashSet<State>();
            TransitionDictionary transitions = new TransitionDictionary();
            //writer.WritePropertyName("transitions");
            //writer.WriteStartArray();

            //foreach (State state in automaton.States)
            //{
            //    writer.WriteStartObject();
            //    writer.WritePropertyName("state");
            //    writer.WriteValue(state);
            //    writer.WritePropertyName("nextStates");
            //    writer.WriteStartArray();
            //    for (int i = 0; i < alphabetSorted.Length; i++)
            //    {
            //        ISet<State> transition = automaton.Transitions[state, alphabetSorted[i]];
            //        writer.WriteStartArray();
            //        foreach (State next in transition)
            //        {
            //            writer.WriteValue(next);
            //        }
            //        writer.WriteEndArray();
            //    }
            //    writer.WriteEndArray();
            //    writer.WriteEndObject();
            //}
            //writer.WriteEndArray();
            //writer.WriteEndObject();
            //writer.Flush();

            //return text.ToString();
            throw new Exception();

        }
    }
}